<template>
	<view class="page-container">
		<!-- 顶部标题栏 -->
		<view class="header">
			<view class="title">我的资金</view>
			<view class="action"></view>
		</view>

		<!-- 资金总览区域 -->
		<view class="balance-section">
			<view class="balance-title">可用余额</view>
			<view class="balance-amount">¥ {{ userinfo.money }}</view>
			<view class="balance-actions">
				<button class="action-btn" type="default" size="mini" @click="handleWithdraw">提现</button>
			</view>
		</view>
		
		<!-- 明细分类标签 -->
		<scroll-view class="filter-tabs" scroll-x>
			<view v-for="(tab, index) in tabs" :key="index" class="tab-item" :class="{ active: activeTab === tab.type }"
				@click="gswitchTab" :data-type="tab.type">
				{{ tab.name }}
			</view>
		</scroll-view>
		
		
		<!-- 资金明细列表 -->
		<scroll-view class="record-list" scroll-y @scrolltolower="loadMore" :style="{ height: scrollHeight + 'px' }">
			<!-- 空状态 -->
			
			<view v-if="records.length === 0" class="empty-state">
				<image src="https://ai-public.mastergo.com/ai/img_res/ab5a40f2f77202861bd59f6c30258b29.jpg"
					class="empty-image" />
				<view class="empty-text">暂无资金记录</view>
			</view>
			
			<!-- 记录列表 -->
			<view v-for="(record, index) in records" :key="index" class="record-item">
				<view class="record-icon">
					<image src="/static/tx.png" mode=""></image>
				</view>
				<view class="record-content">
					<view class="record-title">{{ record.memo }}</view>
					<view class="record-time">{{ record.createtime }}</view>
				</view>
				<view class="record-amount" :style="{ color: record.type === 'income' ? '#07C160' : '#FF4D4F' }">
					{{ record.type === 'income' ? '+' : '-' }}¥{{ record.money }}
				</view>
			</view>
			
			
		</scroll-view>
		
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';

	export default {
		data() {
			return {
				currentBalance: '12',
				activeTab:'all',
				userinfo:[],
				tabs: [{
						name: '全部',
						type: 'all'
					},
					{
						name: '收入',
						type: 'income'
					},
					{
						name: '支出',
						type: 'expense'
					}
				],
				records: []
			}
		},
		onShow() {
			this.getalllistbytype();
			this.getmyinfo();
				
			this.getmygetmoney();	 
			
		},
		methods: {
			
			async getmygetmoney(){
				
				var that=this;
				let product = await this.$api.request('/money/getwxshoukuan', 'POST');
				
				if	(product){
					
					wx.requestMerchantTransfer({
					   mchId: '1676098779',
					   appId: 'wxc54a4da93113e17c',
					   package: product.package_info,
					   success: (res) => {
					     that.setwxmoney(product.id);
					   },
					   fail: (res) => {
					     console.log('fail:', res);
					   },
					 });
					
					
					
				}
				
			},
			async setwxmoney(id){
				
				await this.$api.request('/money/setwxshoukuan', 'POST',{id:id});
			},
			
			async gswitchTab(ret){
				console.log(ret);
				var t=ret.currentTarget.dataset.type;
				
				this.activeTab=t;
				this.getalllistbytype();
			},
			async handleWithdraw(){
				var that=this;
				
				uni.showModal({
					title: '提示',
					content: '确认申请提现？',
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							
							that.getmymoney();
							
						} 
					}
				})
			},
			async getmymoney(){
					
				var that=this;
				let product = await this.$api.request('/money/getmymoney', 'POST');
			
			console.log('=========',product)
			
				if(product.code==1){
					uni.showToast({
						title:product.msg,
						icon:'none'
					})
					
					that.getalllistbytype();
					that.getmyinfo();
					
				}else{
					return false;
				}
				console.log(product)
			},
			async getalllistbytype(){
				
				console.log('*******');
				var that=this;
				var activeTab=this.activeTab;
				
				let product = await this.$api.request('/money/getmymoneylog', 'POST',{'types':activeTab});
			
				that.records=product;
				
				console.log('88888',product)
			},
			async getmyinfo(){
				
				let user = await this.$api.request('/user/userinfo', 'POST',{});
					
					console.log(user);		
				this.userinfo=user;
				
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
		background-color: #f7f7f7;
	}

	.page-container {
		display: flex;
		flex-direction: column;
		height: 100%;
		background-color: #f7f7f7;
	}

	/* 顶部标题栏样式 */
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 30rpx;
		background-color: #fff;
		position: relative;
		z-index: 10;
	}

	.header .title {
		font-size: 36rpx;
		font-weight: 600;
		color: #333;
	}

	.header .action {
		width: 40rpx;
		height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	/* 资金总览区域样式 */
	.balance-section {
		background-color: #fff;
		padding: 20rpx 10rpx;
		margin-bottom: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.balance-title {
		font-size: 28rpx;
		color: #999;
		margin-bottom: 10rpx;
	}

	.balance-amount {
		font-size: 64rpx;
		font-weight: 600;
		color: #333;
		margin-bottom: 30rpx;
	}

	.balance-actions {
		display: flex;
		justify-content: center;
		gap: 30rpx;
		width: 100%;
	}

	.action-btn {
		flex: 1;
		max-width: 200rpx;
		line-height: 60rpx;
		height: 60rpx;
	}

	/* 筛选标签样式 */
	.filter-tabs {
		white-space: nowrap;
		background-color: #fff;
		padding: 20rpx 30rpx;
		margin-bottom: 20rpx;
	}

	.tab-item {
		display: inline-block;
		padding: 10rpx 30rpx;
		margin-right: 20rpx;
		font-size: 28rpx;
		color: #666;
		border-radius: 30rpx;
		background-color: #f5f5f5;
	}

	.tab-item.active {
		background-color: #1890ff;
		color: #fff;
	}

	/* 记录列表样式 */
	.record-list {
		flex: 1;
		background-color: #fff;
		padding: 0 30rpx;
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
	}

	.empty-image {
		width: 300rpx;
		height: 300rpx;
		margin-bottom: 30rpx;
	}

	.empty-text {
		font-size: 28rpx;
		color: #999;
	}

	.record-item {
		display: flex;
		align-items: center;
		padding: 30rpx 0;
		border-bottom: 1px solid #f0f0f0;
	}

	.record-item:last-child {
		border-bottom: none;
	}

	.record-icon {
		width: 58rpx;
		height: 58rpx;
		margin-right: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.record-icon>image{
		width: 58rpx;
		height: 58rpx;
	}
	

	.record-content {
		flex: 1;
	}

	.record-title {
		font-size: 30rpx;
		color: #333;
		margin-bottom: 8rpx;
	}

	.record-time {
		font-size: 24rpx;
		color: #999;
	}

	.record-amount {
		font-size: 32rpx;
		font-weight: 600;
	}

	.loading-more {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 30rpx 0;
		font-size: 24rpx;
		color: #999;
	}

	.loading-more text {
		margin-left: 10rpx;
	}

	/* 筛选弹窗样式 */
	.filter-popup {
		background-color: #fff;
		border-radius: 16rpx 16rpx 0 0;
		padding: 30rpx;
	}

	.popup-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.popup-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #333;
	}

	.popup-content {
		margin-bottom: 40rpx;
	}

	.filter-option {
		margin-bottom: 30rpx;
	}

	.filter-option text {
		display: block;
		font-size: 28rpx;
		color: #666;
		margin-bottom: 15rpx;
	}

	.picker {
		padding: 20rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: #333;
	}

	.amount-range {
		display: flex;
		align-items: center;
	}

	.amount-input {
		flex: 1;
		padding: 20rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: #333;
		text-align: center;
	}

	.amount-range text {
		margin: 0 20rpx;
		font-size: 28rpx;
		color: #666;
	}

	.popup-footer {
		display: flex;
		justify-content: space-between;
	}

	.popup-footer button {
		flex: 1;
		margin: 0 15rpx;
	}
</style>